package org.jeecg.modules.demo.zc.carCost.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.zc.carCost.entity.CarCost;
import org.jeecg.modules.demo.zc.carCost.service.ICarCostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 车辆费用支出
 */
@Api(tags="CarCost")
@RestController
@RequestMapping("/zc/CarCost")
@Slf4j
public class CarCostController extends JeecgController<CarCost, ICarCostService> {

    @Autowired
    private ICarCostService carCostService;

    /**
     * 分页列表查询
     *
     * @param carCost
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "carCost-分页列表查询")
    @ApiOperation(value="carCost-分页列表查询", notes="carCost-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<CarCost>> queryPageList(CarCost carCost,
                                                @RequestParam(name="orgCode", required = false) String orgCode,
                                                @RequestParam(name="userid") String userid,
                                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                HttpServletRequest req) {

        if(orgCode == null){
            orgCode = "";
        }

        Integer orgId = carCostService.getOrgId(orgCode);
        List<Integer> orgIdList = carCostService.getparentOrgIds(orgId);
        orgIdList.add(orgId);
        String orgIdsStr = orgIdList.stream()
                .map(Object::toString)
                .collect(Collectors.joining(","));

        Page<CarCost> page = new Page<CarCost>(pageNo, pageSize);
        IPage<CarCost> pageList = carCostService.getCostSum(page,orgIdsStr);

        return Result.OK(pageList);
    }

    /**
     * 通过车牌号查询
     *
     * @param carCost
     * @return
     */
    //@AutoLog(value = "tradeorder-通过id查询")
    @ApiOperation(value="carCost-通过plateNumber查询", notes="carCost-通过plateNumber查询")
    @GetMapping(value = "/queryByPlateNumber")
    public Result<IPage<CarCost>> queryById(CarCost carCost,
                                     @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                     HttpServletRequest req) {
        QueryWrapper<CarCost> queryWrapper = QueryGenerator.initQueryWrapper(carCost, req.getParameterMap());
        queryWrapper.eq("PLATE_NUMBER",carCost.getPlateNumber());
        Page<CarCost> page = new Page<CarCost>(pageNo, pageSize);
        IPage<CarCost> pageList = carCostService.page(page, queryWrapper);

        return Result.OK(pageList);
    }

}
